#!/bin/sh

# docker run -ti -p 2181 -p 2888 -p 3888 --name node1 flume_hadoop_kafka_storm_zk /bin/bash
# usage ./thisfile.sh order ip1 ip2 ip3
# ./thisfile.sh 1 192.168.1.1  192.168.1.2 192.168.1.3
order=$1
h1=$2
h2=$3
h3=$4

# ========================zookeeper config =================
zkCfg="
tickTime=2000\n
initLimit=10\n
syncLimit=5\n
dataDir=/var/lib/zookeeper\n
clientPort=2181\n 
server.1=$h1:2888:3888\n 
server.2=$h2:2888:3888\n
server.3=$h3:2888:3888
"
touch /opt/zookeeper/conf/zoo.cfg
echo $zkCfg > /opt/zookeeper/conf/zoo.cfg

mkdir /var/lib/zookeeper
touch /var/lib/zookeeper/myid
echo $order > /var/lib/zookeeper/myid

# check
# echo '-----/var/lib/zookeeper/myid'
# cat /var/lib/zookeeper/myid
# echo '-----/opt/zookeeper/conf/zoo.cfg'
# cat /opt/zookeeper/conf/zoo.cfg





# ========================kafka  config =================
id=$(($order-1))
myip=
while IFS=$': \t' read -a line ;do
    [ -z "${line%inet}" ] && ip=${line[${#line[1]}>4?1:2]} &&
        [ "${ip#127.0.0.1}" ] && myip=$ip
  done< <(LANG=C /sbin/ifconfig)
#echo $myip
# host.name=$myip\n

kafkaCfg="
broker.id=$id\n

port=9092\n
num.network.threads=3\n
num.io.threads=8\n
socket.send.buffer.bytes=102400\n
socket.receive.buffer.bytes=102400\n
socket.request.max.bytes=104857600\n
log.dirs=/tmp/kafka-logs\n
num.partitions=1\n
num.recovery.threads.per.data.dir=1\n
log.retention.hours=168\n
log.segment.bytes=1073741824\n
log.retention.check.interval.ms=300000\n
log.cleaner.enable=false\n
zookeeper.connect=$h1:2181,$h2:2181,$h3:2181/kafka\n
zookeeper.connection.timeout.ms=6000
"
echo $kafkaCfg > /opt/kafka/config/server.properties
# cat /opt/kafka/config/server.properties
# need startup mannual 


# ===================start zookeeper================
/opt/zookeeper/bin/zkServer.sh start
# /opt/zookeeper/bin/zkServer.sh status
# /opt/zookeeper/bin/zkCli.sh

# ===================start kafka================
# cat /opt/kafka/config/server.properties
/opt/kafka/bin/kafka-server-start.sh -daemon /opt/kafka/config/server.properties


# ===================start storm================
# cat /opt/storm/conf/storm.yaml
/opt/storm/bin/storm nimbus &
/opt/storm/bin/storm supervisor &
/opt/storm/bin/storm ui &
